<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable|\Illuminate\Http\RedirectResponse
     */
    public function index()
    {
        /** @var User $user */
        $user = Auth::user();

        // 如果有考试进行中，直接进入答题页
        if ($user->currentTest()) {
            return redirect()->route('test');
        }

        // 已参加考试次数
        $testCount = $user->test()->count();

        // 选题数
        $selectedCount = Category::getFromCache()->sum('selected_count');

        // 考试时间，每题分值等设置
        $setting = admin_setting();

        return view('home', compact('user', 'testCount', 'selectedCount', 'setting'));
    }
}
